package io.renren.modules.work.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.ConfigConstant;
import io.renren.common.utils.R;
import io.renren.modules.work.entity.ScExpensedtlEntity;
import io.renren.modules.work.entity.ScProjectdtlEntity;
import io.renren.modules.work.service.ScExpensedtlService;
import io.renren.modules.work.service.ScProjectdtlService;
import io.renren.vo.ExpenseVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.work.dao.ScExpenseDao;
import io.renren.modules.work.entity.ScExpenseEntity;
import io.renren.modules.work.service.ScExpenseService;
import org.springframework.transaction.annotation.Transactional;


@Service("scExpenseService")
public class ScExpenseServiceImpl extends ServiceImpl<ScExpenseDao, ScExpenseEntity> implements ScExpenseService {

    @Autowired
    private ScProjectdtlService scProjectdtlService;

    @Autowired
    private ScExpensedtlService scExpensedtlService;

    @Autowired
    private ScExpenseDao scExpenseDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //时间判断
        String sTime = null;
        String eTime = null;
        if(StringUtils.isNotBlank((String) params.get("sTime"))&&StringUtils.isNotBlank((String) params.get("eTime"))){
            sTime = sdf.format( Long.parseLong((String) params.get("sTime")));
            eTime = sdf.format( Long.parseLong((String) params.get("eTime")));
        }
        IPage<ExpenseVo> page = scExpenseDao.selectExpenseVolist(new Query<ExpenseVo>().getPage(params), (String) params.get("expensename"),(String) params.get("projectname")
                ,(String) params.get("projectid"),
                (String) params.get("cretimeSort"),
                (String) params.get("priceSort"),
                sTime,
                eTime);
        return new PageUtils(page);
    }

    @Override
    public R mSave(String params) {
        ExpenseVo expenseVo = JSONObject.parseObject(params, ExpenseVo.class);
        String id = ConfigConstant.getId();
        ScExpenseEntity scExpenseEntity = new ScExpenseEntity();
        scExpenseEntity.setExpenseid(id);
        scExpenseEntity.setExpensename(expenseVo.getExpensename());
        scExpenseEntity.setProjectid(expenseVo.getProjectid());
        scExpenseEntity.setProjectdtlid(expenseVo.getProjectdtlid());
        scExpenseEntity.setPrice(expenseVo.getPrice());
        scExpenseEntity.setCretime(new Date());
        baseMapper.insert(scExpenseEntity);
        List<ScExpensedtlEntity> expensedtl = expenseVo.getExpensedtl();
        for (ScExpensedtlEntity scExpensedtlEntity : expensedtl) {
            scExpensedtlEntity.setCretime(new Date());
            scExpensedtlEntity.setExpendseid(id);
            scExpensedtlService.save(scExpensedtlEntity);
        }
        return R.ok();
    }
    @Transactional
    @Override
    public R mUpdate(String params) {
        ExpenseVo expenseVo = JSONObject.parseObject(params, ExpenseVo.class);
        ScExpenseEntity scExpenseEntity = baseMapper.selectById(expenseVo.getExpenseid());
        scExpenseEntity.setPrice(expenseVo.getPrice());
        scExpenseEntity.setExpensename(expenseVo.getExpensename());
        baseMapper.updateById(scExpenseEntity);
        List<ScExpensedtlEntity> expensedtl = expenseVo.getExpensedtl();
        //删除列表中不存在的细单
        List<String> ids = new ArrayList<>();
        for (ScExpensedtlEntity scExpensedtlEntity : expensedtl) {
            if(scExpensedtlEntity.getExpendsedtlid()!=null){
                ids.add(scExpensedtlEntity.getExpendsedtlid());
            }
        }
        QueryWrapper<ScExpensedtlEntity> scExpensedtlEntityQueryWrapper = new QueryWrapper<>();
        scExpensedtlEntityQueryWrapper.eq("expendseid",expenseVo.getExpenseid());
        if(ids.size()>0){
            scExpensedtlEntityQueryWrapper.notIn("expendsedtlid",ids);
        }
        scExpensedtlService.remove(scExpensedtlEntityQueryWrapper);

        for (ScExpensedtlEntity scExpensedtlEntity : expensedtl) {
            if(StringUtils.isNotBlank(scExpensedtlEntity.getExpendsedtlid())){
                scExpensedtlService.updateById(scExpensedtlEntity);
            }else{
                scExpensedtlEntity.setExpendseid(expenseVo.getExpenseid());
                scExpensedtlService.save(scExpensedtlEntity);
            }

        }
        return R.ok();
    }

    @Override
    public R mInfo(String expenseid) {
        ScExpenseEntity scExpenseEntity = baseMapper.selectById(expenseid);
        ExpenseVo expenseVo = new ExpenseVo();
        BeanUtil.copyProperties(scExpenseEntity,expenseVo);
        QueryWrapper<ScExpensedtlEntity> scExpensedtlEntityQueryWrapper = new QueryWrapper<>();
        scExpensedtlEntityQueryWrapper.eq("expendseid",expenseid);
        List<ScExpensedtlEntity> list = scExpensedtlService.list(scExpensedtlEntityQueryWrapper);
        expenseVo.setExpensedtl(list);
        return R.ok().put("data",expenseVo);
    }

}